//! Implementation of the OdometerObserver class
/*!
	\file OdometerObserver.cpp
	\author Andreas Lehner
	\date 22.11.2012

*/
#include "OdometerObserver.h"

//constants for distance calculation
double const OdometerObserver::cMeterfactor = 3.6;
double const OdometerObserver::cHalfsec = 0.5;

OdometerObserver::OdometerObserver(PKW * automobile) : mDistance(0) {
	mAutomobile = automobile;
	//login -> registrate in observer list
	mAutomobile->Attach(this);
	mDisplay = new DigitalDisplay();
}

OdometerObserver::~OdometerObserver(){
	//logout -> leave observer list
	mAutomobile->Detach(this);
	mAutomobile = 0;
	delete mDisplay; mDisplay = 0;
}

void OdometerObserver::Update(){
	//calculate distance in m/s (update every half second)
	mDistance += (mAutomobile->GetCurrentSpeed() / cMeterfactor) * cHalfsec;
	//update value on an digital display
	if (!mDisplay->SendValue((int)mDistance)){
		throw std::exception("Distance value could not be sent to DigitalDisplay!");
	}
}